package com.keshan.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.keshan.pojo.*;
import com.keshan.service.HeartrateServices;
import com.keshan.service.UserDeviceServices;
import com.keshan.utils.DateUtil;
import com.keshan.utils.JWTUtils;
import com.keshan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author keshan
 * @date 2022/10/21 - 19:04
 */
@CrossOrigin
@Controller
@RequestMapping("/heartrate")
public class HeartrateController {
    @Autowired
    HeartrateServices heartrateServices;
    @Autowired
    UserDeviceServices userDeviceServices;

    @RequestMapping("/insertheartrate")
    @ResponseBody
    public Msg insertBySelectiveToHeartrate(@RequestBody HeartrateJson heartrateJson) {
        Date date = new Date();
        System.out.println(heartrateJson.toString());
        System.out.println(date.toString());
        List<UserDevice> userDeviceList = userDeviceServices.getUserDeviceListByImei(heartrateJson.getImei());
        try {
            Heartrate heartrate = new Heartrate();
            heartrate.setDeviceType(heartrateJson.getDevice_type());
            heartrate.setHeartrate(heartrateJson.getHeartrate());
            heartrate.setUsername(userDeviceList.get(0).getUsername());
            heartrate.setThesholdHeartrateH(heartrateJson.getTheshold_heartrate_h());
            heartrate.setThesholdHeartrateL(heartrateJson.getTheshold_heartrate_l());
            heartrate.setTimeBegin(DateUtil.StringToDate(heartrateJson.getTime_begin_str()));
            System.out.println(heartrate.toString());
            int result = heartrateServices.insertBySelectiveHeartrate(heartrate);
            return Msg.success().add("result", result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Msg.failInsert();
    }

    @RequestMapping("/heartratelisthidetoday")
    @ResponseBody
    public Msg selectHeartrateListHideByImeiToday(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
            List<HeartrateHide> heartrateList = heartrateServices
                    .getHeartrateListHideByUsernameToday(username);
            return Msg.success().add("heartrateList", heartrateList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }

    /**
     * 查询当年前后两个月每日数据平均值
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping("/heartratelistdayavg")
    @ResponseBody
    public Msg selectHeartrateListByImeiDayAvg(HttpServletRequest httpServletRequest) {
        Map<String, String> map = new HashMap<>();
        String token = httpServletRequest.getHeader("token");
        System.out.println(token);
        try {
            DecodedJWT decodedJWT = JWTUtils.verifyAndInfo(token);
            String username = decodedJWT.getClaim("username").asString();
            System.out.println(username);
            List<HeartrateDayAvg> heartrateDayAvgList = heartrateServices
                    .getHeartrateListEverydayAvgMonth(username);
            return Msg.success().add("heartrateDayAvgList", heartrateDayAvgList);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Msg.fail().add("error", "查询失败");
    }
}
